package com.cncbox.service.impl;

import com.cncbox.common.BeanUtil;
import com.cncbox.common.Utils;
import com.cncbox.dao.ArticleDao;
import com.cncbox.dao.ResourceDao;
import com.cncbox.dto.EnhanceArticle;
import com.cncbox.entity.Article;
import com.cncbox.entity.NoteResult;
import com.cncbox.entity.PagedResult;
import com.cncbox.entity.Resource;
import com.cncbox.service.ArticleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Develop_Peng
 * @version V1.0
 * @Package com.cncbox.service.impl
 * @Description: 文章服务类实现类
 * @date 2016/3/31 11:34
 */
public class AtrcleServiceImpl implements ArticleService {

    //文章
    @Autowired
    private ArticleDao articleDao;

    //资源
    @Autowired
    private ResourceDao resourceDao;

    //添加文章类
    public NoteResult addArticle(EnhanceArticle enhanceArticle) throws Exception {
        //初始化返回数据
        NoteResult noteResult = new NoteResult();
        //接收传递数据
        String titleName = enhanceArticle.getTitleName().trim();
        String oneClass = enhanceArticle.getOneClass().trim();
        String twoClass = enhanceArticle.getTwoClass().trim();
        String content = enhanceArticle.getContent();
        String detailed = enhanceArticle.getDetailed();


        char thisState = enhanceArticle.getThisState();
        Integer sorting = enhanceArticle.getSorting();

        char fileType = enhanceArticle.getFileType();
        String clickPath = enhanceArticle.getClickPath();
        String filePath = enhanceArticle.getFilePath();

        //进行封装
        Article article = new Article();
        article.setTitleName(titleName);
        article.setOneClass(oneClass);
        article.setTwoClass(twoClass);
        article.setContent(content);
        article.setDetailed(detailed);
        article.setThisState(thisState);
        article.setSorting(sorting);

        //主键初始化
        Integer articleKay = 0;
        //进行文章的添加
        Integer isAdd = articleDao.addArticle(article);
        //判断添加是否成功
        if (0 < isAdd)
            //将新添加的数据进行返回
            articleKay = articleDao.selectArticleIsTrue(article);
        Resource resource = new Resource();
        resource.setName(titleName);
        resource.setArtId(articleKay);
        resource.setType(fileType);
        resource.setPathName(filePath);
        resource.setState(thisState);
        resource.setSorting(sorting);
        resource.setClickPath(clickPath);
        resource.setAddTime(Utils.format(new Date()));
        //进行资源的添加
        Boolean resourceResult = resourceDao.addResource(resource);

        if (resourceResult) {
            noteResult.setStatus(1);
            noteResult.setMsg("ADD_SUCCESS");
        } else {
            noteResult.setStatus(0);
            noteResult.setMsg("ADD_ERROR");
        }
        return noteResult;
    }

    //根据分类获取数据列表
    public NoteResult selectOrderByClass(Article articles) throws Exception {
        NoteResult noteResult = new NoteResult();
        //接收返回数据
        List<Article> article = articleDao.selectClassData(articles);

        //进行返回数据的判断
        if (article != null || article.size() > 0) {
            noteResult.setStatus(1);
            noteResult.setMsg("FIND_SUCCESS");
            noteResult.setData(article);
        } else {
            noteResult.setStatus(0);
            noteResult.setMsg("FIND_ERROR");
        }
        return noteResult;
    }

    //修改前的查询数据
    public NoteResult queryArticleById(Integer id) throws Exception {
        NoteResult noteResult = new NoteResult();
        Resource resource = resourceDao.queryResource(id);
        if (resource != null) {
            //获取Id
            Integer artId = resource.getArtId();
            if (artId != null || 0 < artId) {
                Article article = articleDao.queryArticle(artId);
                if (article != null) {
                    Map<String, Object> objectMap = new HashMap<String, Object>();
                    objectMap.put("Resource", resource);
                    objectMap.put("Article", article);
                    noteResult.setStatus(1);
                    noteResult.setMsg("QUERY_SUCCESS");
                    noteResult.setData(objectMap);
                } else {
                    noteResult.setStatus(0);
                    noteResult.setMsg("QUERY_ERROT");
                }
            } else {
                noteResult.setStatus(1);
                noteResult.setMsg("QUERY_SUCCESS");
            }
        } else {
            noteResult.setStatus(1);
            noteResult.setMsg("QUERY_SUCCESS");
        }
        return noteResult;
    }

    //修改文章类
    public NoteResult updateByResourceId(EnhanceArticle enhanceArticle) throws Exception {

        //初始化返回数据
        NoteResult noteResult = new NoteResult();
        //接收传递数据
        String titleName = enhanceArticle.getTitleName();
        String content = enhanceArticle.getContent();
        String detailed = enhanceArticle.getDetailed();
        char thisState = enhanceArticle.getThisState();
        Integer sorting = enhanceArticle.getSorting();
        //查询是否存在资源
        Resource resource = resourceDao.queryResource(enhanceArticle.getResourceId());
        //将返回文章Id存入
        Integer artId = resource.getArtId();
        if (artId != null || 0 < artId) {
            Article article = new Article();
            article.setId(artId);
            article.setTitleName(titleName);
            article.setContent(content);
            article.setDetailed(detailed);
            article.setThisState(thisState);
            article.setSorting(sorting);
            Boolean aBoolean = articleDao.updateArticle(article);
            if (enhanceArticle.getFilePath() != null && !"".equals(enhanceArticle.getFilePath())) {
                Resource resource1 = new Resource();
                resource1.setId(enhanceArticle.getResourceId());
                resource1.setPathName(enhanceArticle.getFilePath());
                resource1.setName(enhanceArticle.getTitleName());
                Boolean resource2 = resourceDao.updateResourcePathNameById(resource1);
                if (resource2) {
                    noteResult.setMsg("UPDATE_IMG_SUCCESS");
                    noteResult.setStatus(1);
                }
            }
            noteResult.setMsg("UPDATE_SUCCESS");
            noteResult.setStatus(1);
        } else {
            noteResult.setMsg("UPDATE_ERROR");
            noteResult.setStatus(0);
        }
        return noteResult;
    }


    //根据Id删除文章
    public NoteResult removeArticleById(Integer id) throws Exception {
        NoteResult noteResult = new NoteResult();

        Boolean deleteArticle = articleDao.deleteArticle(id);
        Boolean deleteResourceByArtId = resourceDao.deleteResourceByArtId(id);
        if (deleteArticle && deleteResourceByArtId) {
            noteResult.setStatus(1);
            noteResult.setMsg("DELETE_SUCCESS");
        } else {
            noteResult.setStatus(0);
            noteResult.setMsg("DELETE_ERROR");
        }
        return noteResult;
    }

    //带分页、分类、状态来获取数据列表
    public NoteResult selectStatusOrderByClass(Article article, Integer pageNo, Integer pageSize) throws Exception {
        NoteResult noteResult = new NoteResult();
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 5 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        PagedResult<Article> articleContext = BeanUtil.toPagedResult(articleDao.selectStatusClassData(article));
        //向二级分类添加图片查询字段
        article.setTwoClass("Image");
        Article articleTopImage = articleDao.selectTopImageClassData(article);

        Map<String, Object> articleMap = new HashMap<String, Object>();
        articleMap.put("articleContext", articleContext);
        //将图片名直接返回
        articleMap.put("topImagePath", articleTopImage.getResource().get(0).getPathName());
        if (articleContext != null) {
            noteResult.setStatus(1);
            noteResult.setMsg("FIND_SUCCESS");
            noteResult.setData(articleMap);
        } else {
            noteResult.setStatus(0);
            noteResult.setMsg("FIND_ERROR");
        }
        return noteResult;
    }

    //根据id查询文章
    public NoteResult queryContentById(Integer id) throws Exception {
        NoteResult result = new NoteResult();
        Article article = articleDao.queryArticle(id);
        if (article != null) {
            result.setMsg("QUERY_SUCCESS");
            result.setStatus(0);
            result.setData(article);
        } else {
            result.setMsg("QUERY_ERROR");
            result.setStatus(0);
        }
        return result;
    }


}
